define(['app'], function(app) {
	'use strict';

	var PAGE_LIMIT = 15;

	var SaveData = null;

	var controller = function controller($scope, Favorite, $window, $timeout, $ionicSlideBoxDelegate, $ionicScrollDelegate, GoPage, GoDialog) {

		function init_scope() {
			$scope.data = {};
			$scope.data = {
				goods: {
					inited: false, isend: false,
					cPage: 1, tPage: null,
					list: []
				},
				store: {
					inited: false, isend: false,
					cPage: 1, tPage: null,
					list: []
				}
			}
			$scope.data.selectIndex = 0;
			SaveData = $scope.data;
		}

		//画面加载
		$scope.$on('$ionicView.afterEnter', function(scope, status) {
			//动态设置高度
			var all = $window.innerHeight;
			// 计算滚动条
			$scope.myObj = {
				"height": all - 44 -0 + "px"
			};
			if (status.direction != 'back') {
				$scope.isLoad = false; //可以加载
				init_scope();
				$scope.init_page(0, true);
			} else {
				$scope.data = SaveData;
				$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.data.selectIndex);
			}
		}, false);


		//滑动时候触发		
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.data.selectIndex = index;
			var data = (index==0) ? $scope.data.goods : $scope.data.store;
			if (data && !data.inited) {
				$scope.init_page(index, true);
			}
		};
		$scope.activeSlide = function(index) {
			$scope.data.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};

		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.init_page($scope.data.selectIndex, true, 'refresh');
		};

		//加载更多
		$scope.doRefreshMore = function() {
			if ($scope.isLoad) return;
			$scope.isLoad = true;
			$scope.init_page($scope.data.selectIndex, false, 'more');
		};

		$scope.init_page = function(index, isInit, completeFlag) {
			var data = (index==0) ? $scope.data.goods : $scope.data.store;
			if (isInit) data.isend = false;

			var param = {
				pagelimit: PAGE_LIMIT,
				nPage: isInit ? 1 : (data.cPage - 0 + 1)
			};

			var fnFavo = (index==0) ? Favorite.get_goods : Favorite.get_store;
			fnFavo(param, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}
				var result = res.result;
//				console.log(result)
				if(result==null){
					return;
				}
				data.cPage = result.page.cPage;
				data.tPage = result.page.tPage;
				var rltList = (index==0) ? (result.goods || []) : (result.shops || []);
				if (data.cPage == data.tPage) data.isend = true;
				if (isInit && data.inited) {
					if (completeFlag != 'refresh')	{
						var name = (index == 0) ? 'scroll0' : 'scroll1';
						$ionicScrollDelegate.$getByHandle(name).scrollTop();
					}
					data.list = rltList;
				} else {
					if (param.nPage == 1) {
						data.list = rltList;
					} else {
						for (var i=0; i<rltList.length; i++) {
							data.list.push(rltList[i]);
						} 
					}
				}
				data.inited = true;
				
			}, null, function() {
				if (completeFlag === 'refresh')	{
					$scope.$broadcast('scroll.refreshComplete');
				}
				if (completeFlag === 'more') {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				}
			})
		}

		$scope.delete_favo = function(index, tabIndex, $event) {
			$event.stopPropagation();
			var data = (tabIndex==0) ? $scope.data.goods : $scope.data.store;
			if (!data.list[index]) return;
			var param = {}
			if (tabIndex==0) {
				param.goods_id = data.list[index].goods_id;
			} else {
				param.store_id = data.list[index].store_id
			}
			var fnFavo = (tabIndex==0) ? Favorite.delete_goods : Favorite.delete_store;
			fnFavo(param, function(res) {
				if (res.success != true) {
					GoDialog.toast(res.msg);
					return;
				}
				data.list.splice(index, 1);
			});
		}

		//页面跳转
		$scope.onshow_detail = function(goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, "type": "goods" });
		};
		$scope.onshow_first = function(store_id,$event) {
			$event.stopPropagation();//阻止事件的冒泡
			GoPage.go_states("first", {"store_id": store_id});
		}

	}

	controller.$inject = ['$scope', 'Favorite', '$window', '$timeout', '$ionicSlideBoxDelegate', '$ionicScrollDelegate','GoPage', 'GoDialog'];
	
	return app.registerController("favoritectrl",controller);
});